package com.zxx.xin.shorthand.database;

import android.content.Context;

import com.zxx.xin.shorthand.database.bean.Daily;
import com.zxx.xin.shorthand.database.bean.DailyDao;
import com.zxx.xin.shorthand.database.bean.DaoMaster;
import com.zxx.xin.shorthand.database.bean.DaoSession;

import java.util.List;

/**
 * Created by xin on 2018/3/17.
 */

public class DatabaseHelper {

    private static Context sContext;

    private DailyDao mDailyDao;

    private static DatabaseHelper sInstance;

    private DatabaseHelper(Context context){
        DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(context, "daily.db");
        DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDb());
        DaoSession daoSession = daoMaster.newSession();
        mDailyDao = daoSession.getDailyDao();
    }

    public static void init(Context context){
        if(sInstance == null){
            sContext = context;
        }
    }

    public static DatabaseHelper getInstance(){
        if (sInstance == null){
            sInstance = new DatabaseHelper(sContext);
        }
        return sInstance;
    }

    public void insertDaily(Daily daily){
        mDailyDao.insert(daily);
    }

    public void updateDaily(Daily daily){
        mDailyDao.update(daily);
    }

    public void deleteDaily(Daily daily){
        mDailyDao.delete(daily);
    }

    public Daily queryDailyById(Long id){
        return mDailyDao.queryBuilder().where(DailyDao.Properties.Id.eq(id)).unique();
    }

    public List<Daily> listAllDaily(){
        return mDailyDao.queryBuilder().list();
    }

}
